package cn.eeepay.lib_core.ktx

import android.view.View
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/*
  * ================================================
  * 描述：基于作者的BaseMultiItemQuickAdapter 抽象出多布局共性的adapter，从此可以避免写adapter的烦恼了
  * 多类型布局，适用于类型较少，业务不复杂的场景，便于快速使用。 dataT必须实现MultiItemEntity接口
  * 作者：zhuangzeqin
  * 时间: 2023年11月13日15:54:46
  * 邮箱：zzq@eeepay.cn
  * 备注:
  * ----------------------------------------------------------------
  * You never know what you can do until you try !
  *      _              _           _     _   ____  _             _ _
  *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
  *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
  *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
  *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
  *
  * 签名：最痛苦的事不是我失败了，而是我本可以.--zzq
  * ----------------------------------------------------------------
  * ================================================
  *  @Keep
        data class AccountInfo(
            val accountNo: String = "",
            val accountTypeNo: String = "",
            val accountTypeName: String = "",
            val balance: String = "",//余额 by
            val canCashAmount: String = "",//可提现金额
            val withdrawalBalance: String = "",//可提现余额
            val createTime: String = "",
            val freezeAmount: String = "",//预冻
            val lastAccountSerialNo: String = "",
            val preFreezeAmount: String = "",//预冻结金额
            val status: String = "",
            val totalAmount: String = "",//总入账金额
            val userNo: String = "",
            var showItemType:Int= 0,//todo 自定义显示类型
            var checked:Boolean= false,//是否选中；默认是false
            val debtAmount: String = "",//待下发欠款

        ): Serializable, MultiItemEntity {
            override val itemType: Int
                get() = showItemType//todo 显示的类型
        }
  * eg:
  *     val list = listOf<AccountInfo>()
        dataBinding.rvList.linear(可选).divider(可选).setMoreLayout<MyMainInfo.Data.AccountInfo>(
        * R.layout.agent_home_item_card_account_small,
        * R.layout.agent_home_item_card_account_big)
        * { holder, data ->
        *  todo 2 根据返回的 type 分别设置数据
                when (holder.itemViewType) {
                    0 -> {//small}
                    1 -> {//big}
                   }
        }.setItemClick { adapter, view, position ->
            adapter.getItem(position).toString().show()
        }.setItemChildClickView(R.id.tvTransactionType) { adapter, view, position ->
            adapter.getItem(position).toString().show()
        }.setList(list)
  */
class CommonBaseMultiItemAdapter<T : MultiItemEntity> constructor(
    layoutResIdList: List<Int>,
) : BaseMultiItemQuickAdapter<T, BaseViewHolder>() {
    init {
        //todo 这里循环添加布局
        layoutResIdList.forEachIndexed { index, itemInfo ->
            addItemType(index, itemInfo)
        }
    }

    private var bindDataFun: ((BaseViewHolder, T) -> Unit)? = null

    /**
     * 绑定数据
     */
    fun bindData(block: ((BaseViewHolder, T) -> Unit)) {
        bindDataFun = block
    }

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * 实现此方法，并使用 helper 完成 item 视图的操作
     *
     * @param holder A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    override fun convert(holder: BaseViewHolder, item: T) {
        bindDataFun?.invoke(holder, item)
    }
}

/**
 * 设置适配器
 */
fun <T : MultiItemEntity> RecyclerView.setMoreLayout(@LayoutRes vararg layoutResId: Int, block: ((BaseViewHolder, T) -> Unit)): CommonBaseMultiItemAdapter<T> {
    val adapter = CommonBaseMultiItemAdapter<T>(layoutResId.toList())
    adapter.bindData(block)
    this.adapter = adapter
    return adapter
}

/**
 * 设置item 点击事件
 */
fun <T : MultiItemEntity> CommonBaseMultiItemAdapter<T>.setItemClick(block: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Unit): CommonBaseMultiItemAdapter<T> {
    this.setOnItemClickListener { adapterInfo, v, pos ->
        block.invoke(adapterInfo, v, pos)
    }
    return this
}

/**
 * 设置item 内控件的 点击事件
 */
fun <T : MultiItemEntity> CommonBaseMultiItemAdapter<T>.setItemChildClickView(@IdRes vararg viewIds: Int, block: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Unit): CommonBaseMultiItemAdapter<T> {
    this.getChildClickViewIds().addAll(viewIds.toList())
    this.setOnItemChildClickListener { adapterInfo, v, pos ->
        block.invoke(adapterInfo, v, pos)
    }
    return this
}

/**
 * 设置item 长按点击事件
 */
fun <T : MultiItemEntity> CommonBaseMultiItemAdapter<T>.setItemLongClick(block: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Boolean): CommonBaseMultiItemAdapter<T> {
    this.setOnItemLongClickListener { adapterInfo, v, pos ->
        block.invoke(adapterInfo, v, pos)
    }
    return this
}

/**
 * 设置item 内控件的 长按点击事件
 */
fun <T : MultiItemEntity> CommonBaseMultiItemAdapter<T>.setItemChildLongClickView(@IdRes vararg viewIds: Int, block: (adapter: BaseQuickAdapter<*, *>, view: View, position: Int) -> Boolean): CommonBaseMultiItemAdapter<T> {
    this.getChildLongClickViewIds().addAll(viewIds.toList())
    this.setOnItemChildLongClickListener { adapterInfo, v, pos ->
        block.invoke(adapterInfo, v, pos)
    }
    return this
}